<template>
  <div class="user-layout">
    <el-container>
      <el-header>
        <div class="header-logo">洗衣服务系统</div>
        <el-menu
          mode="horizontal"
          :router="true"
          :default-active="activeIndex"
          background-color="#409EFF"
          text-color="#fff"
          active-text-color="#ffd04b">
          <el-menu-item index="/home">首页</el-menu-item>
          <el-menu-item index="/user/order/create">预约洗衣</el-menu-item>
          <el-menu-item index="/user/order/list">我的订单</el-menu-item>
          <el-menu-item index="/user/promotions">优惠活动</el-menu-item>
        </el-menu>
        <div class="user-info">
          <span>{{ username }}</span>
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              <i class="el-icon-user"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="center">个人中心</el-dropdown-item>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <el-main>
        <slot></slot>
      </el-main>
      <el-footer>
        <div class="footer-content">
          <p>© 2025 洗衣服务系统 版权所有</p>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'UserLayout',
  data() {
    return {
      activeIndex: this.$route.path
    }
  },
  computed: {
    username() {
      const user = JSON.parse(localStorage.getItem('user') || '{}')
      return user.username || '未登录'
    }
  },
  watch: {
    $route(to) {
      this.activeIndex = to.path
    }
  },
  methods: {
    handleCommand(command) {
      if (command === 'center') {
        this.$router.push('/user/center')
      } else if (command === 'logout') {
        this.logout()
      }
    },
    logout() {
      this.$confirm('确认退出登录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem('user')
        this.$message.success('已退出登录')
        this.$router.push('/login')
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.el-header {
  display: flex;
  align-items: center;
  background-color: #409EFF;
  color: #fff;
  height: 60px !important;
  padding: 0;
}
.header-logo {
  width: 200px;
  font-size: 20px;
  font-weight: bold;
  text-align: center;
}
.el-menu {
  flex: 1;
  border-bottom: none;
}
.user-info {
  margin-right: 20px;
  display: flex;
  align-items: center;
}
.user-info span {
  margin-right: 10px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #fff;
}
.el-main {
  padding: 0;
  min-height: calc(100vh - 120px);
}
.el-footer {
  background-color: #f5f5f5;
  height: 60px !important;
  display: flex;
  align-items: center;
  justify-content: center;
}
.footer-content {
  text-align: center;
  color: #909399;
}
</style> 